﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CompanyManagementDLL;
using CompanyManagementDLL.DLL;
using CompanyManagementDLL.MetaData;
using log4net;

namespace CompanyManagement.Controllers
{
    public class RoleController : Controller
    {
        //
        // GET: /Role/

        RoleDLL dllRole = new RoleDLL();
        private static readonly ILog Log = LogManager.GetLogger(typeof(RoleController));

        #region Page View

        public ActionResult Index()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            Utility.setCookiValue();
            List<RoleMetaData> roleList = new List<RoleMetaData>();
            try
            {
                roleList = dllRole.GetRoleList();
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(roleList);
        }

        public ActionResult Create()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            return View();
        }

        public ActionResult Edit(int id)
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            RoleMetaData rolemetadata = new RoleMetaData();
            try
            {
                rolemetadata = dllRole.GetRoleByID(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(rolemetadata);
        }

        #endregion

        #region DML Operation

        [HttpPost]
        public ActionResult Create(RoleMetaData rolemetadta)
        {
            RoleMetaData rolemetadata = new RoleMetaData();
            try
            {
                if (ModelState.IsValid)
                {
                    dllRole.InsertRole(rolemetadta);
                }
                else
                {
                    return View(rolemetadata);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return View(rolemetadata);
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Edit(RoleMetaData rolemetadta)
        {
            RoleMetaData rolemetadata = new RoleMetaData();
            try
            {
                if (ModelState.IsValid)
                {
                    dllRole.UpdateRole(rolemetadta);
                }
                else
                {
                    return View(rolemetadata);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return View(rolemetadata);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            try
            {
                if (!dllRole.IsRoleHasUser(id))
                {
                    dllRole.DeleteRole(id);
                }
                else
                {
                    TempData["HasUser"] = "You cannot delete this role.<b>Role is alredy assign to user.</b>";

                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return RedirectToAction("Index");
        }
        #endregion


    }
}
